﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FastTravel.BusinessLogic;
using FastTravel.DB;
using FastTravel.DB.Enums;

namespace FastTravel.IntranetWeb
{
	public partial class Login : BasePage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			txtUsername.Focus();
		}


		protected void BtnLoginClick(object sender, DirectEventArgs e)
		{
			LoginAction();
		}

		
		[DirectMethod]
		public void LoginAction()
		{
			string login = txtUsername.Text.Trim();
			string password = txtPassword.Text.Trim();
			if (!string.IsNullOrWhiteSpace(login) && !string.IsNullOrWhiteSpace(password))
			{
				if (UserManager.Validate(login, password))
				{
					var currentUser = UserManager.Get(login);

					CurrentUserDTO currentUserDTO = new CurrentUserDTO
					                                	{
					                                		ID = currentUser.ID,
					                                		Name = currentUser.GetFormattedName(),
					                                		Login = currentUser.Login,
					                                		UserRole = currentUser.UserRole
					                                	};
					SetCurrentUser(currentUserDTO);

					FormsAuthentication.RedirectFromLoginPage(login, false);
					Response.Redirect(urlHome);
				}
				else
				{	
					X.Msg.Notify("Сообщение", "Неверный логин или пароль!").Show();
				}
			}
		}

		
	}
}